﻿//---------------------------------------------------------------------
//Copyright (c) 2009, Pierre Milet
//All rights reserved.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
//OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//---------------------------------------------------------------------
using System;
using System.IO;
using System.Xml;
using System.Net;

using Microsoft.XLANGs.BaseTypes;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.Samples.BizTalk.Adapter.Common;

namespace BizMock.Adapter.Runtime
{
    /// <summary>
    /// This class maintains send port properties associated with a message. These properties
    /// will be extracted from the message context for static send ports.
    /// </summary>
    public class BizMockTransmitProperties : ConfigProperties
    {
        private static string protocolAlias = "BizMock://";

		// Handler members...
        private static int batchSize = 50;

		// Endpoint members...
        private bool isTwoWay;		
        private string uri;
        private int timeout = 90;

        private static PropertyBase isSolicitResponseProp = new BTS.IsSolicitResponse();

        // If we needed to use SSO we will need this extra property. It is set in the
        // LocationConfiguration method below.
        // Additionally:
        //   TransmitLocation.xsd in the design-time project must also be edited to
        //   expose the necessary SSO properties.
        //   DotNetFileAsyncTransmitterBatch.cs within the run-time project must be
        //   edited to retrieve and populate the SSOResult class.
        //private string ssoAffiliateApplication;
        //public string AffiliateApplication { get { return ssoAffiliateApplication; } }

        public BizMockTransmitProperties(IBaseMessage message, string propertyNamespace)
        {
            XmlDocument locationConfigDom = null;

            //  get the adapter configuration off the message
            string config = (string) message.Context.Read("AdapterConfig", propertyNamespace);
            this.isTwoWay = (bool) message.Context.Read(isSolicitResponseProp.Name.Name, isSolicitResponseProp.Name.Namespace);


            //  the config can be null all that means is that we are doing a dynamic send
            if (null != config)
            {
                locationConfigDom = new XmlDocument();
                locationConfigDom.LoadXml(config);

                //  For Dynamic Sends the Location config can be null
                //  Location properties - possibly override some handler properties
                this.LocationConfiguration(locationConfigDom);
            }
            else
            {
                this.uri = message.Context.Read("OutboundTransportLocation", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString();
            }
        }

        public BizMockTransmitProperties(string uri)
        {
            this.uri = uri;
            UpdateUriForDynamicSend();
        }
		
		//Handler properties
		public static int BatchSize	{ get { return BizMockTransmitProperties.batchSize; }	}

		//Endpoint properties
        public bool IsTwoWay { get { return this.isTwoWay; } }
        public string Uri { get { return this.uri; } }
        public int Timeout { get { return timeout; } } // in seconds
		
        public static void TransmitHandlerConfiguration(XmlDocument configDOM)
        {
        }

        public void LocationConfiguration (XmlDocument configDOM)
        {
            this.uri = Extract(configDOM, "/Config/uri", String.Empty);
            timeout = int.Parse(Extract(configDOM, "/Config/timeout", "90"));
        }

        public void UpdateUriForDynamicSend()
        {
            // Strip off the adapters alias
            if ( this.uri.StartsWith(protocolAlias, StringComparison.OrdinalIgnoreCase))
            {
                string newUri = this.uri.Substring(protocolAlias.Length);
                this.uri = newUri;
            }
        }

    }
}
